knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message=FALSE) # set options for following chunks

library(here)
library(sf)
library(tmap)
library(tidyverse)

Read in the Data

# read.csv is built in function, read_csv has additional functionality
sf_trees <- read_csv(here("data", "sf_trees", "sf_trees.csv"))

top_5_status <- sf_trees %>% 
  group_by(legal_status) %>% 
  summarize(tree_count = n()) %>% 
  slice_max(tree_count, n = 5) %>% 
  arrange(-tree_count)

# plot top 5 legal status
ggplot(data = top_5_status, aes(x = fct_reorder(legal_status, tree_count), y = tree_count)) +
  geom_col() +
  labs(y = "Tree Count", x = "Legal Status") +
  coord_flip() +
  theme_minimal()

# see unique legal status
# sf_trees$legal_status %>% unique()
# keep observations where legal status is "permitted site" and caretaker is "MTA"
permitted_mta <- sf_trees %>% 
  filter(legal_status == "Permitted Site" & caretaker == "MTA") 

Make some maps

blackwood_acacia_sf <- blackwood_acacia %>% 
  drop_na(longitude, latitude) %>% 
  st_as_sf(coords = c("longitude", "latitude")) # make points from long & lat

st_crs(blackwood_acacia_sf) <- 4326

ggplot(data = blackwood_acacia_sf) +
  geom_sf(color = "darkgreen") +
  theme_minimal()

read in SF streets data

sf_map_sf <- read_sf(here("data", "sf_map", "tl_2017_06075_roads.shp")) %>% 
  st_transform(4326)

# st_crs(sf_map_sf)
ggplot() +
  geom_sf(data = sf_map_sf, size = 0.1, color = "darkgrey") +
  geom_sf(data = blackwood_acacia_sf, color = "red", size = 0.5) +
  theme_void() +
  labs(title = "Blackwood Acascias in San Francisco")

Interactive Map!

tmap_mode("view")
tm_shape(blackwood_acacia_sf) +
  tm_dots()